import { areaList } from '@vant/area-data';
var http = require("../../../utils/https.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        checked: true,
        show: false,
        areaList, // 地址组件
        name: "",
        phone: "",
        details: '',
        address: "",
        userId: '',
        myAddress: {
            city: "",
            cityCode: "",
            createBy: null,
            createTime: null,
            del: 0,
            details: "",
            district: "",
            districtCode: "",
            id: "",
            isDefault: 0,
            province: "",
            provinceCode: "",
            receiverName: null,
            receiverPhone: null,
            remark: null,
            updateBy: null,
            updateTime: null,
            userId: null,
        },


    },

    onChange({ detail }) {
        this.setData({ checked: detail });
        console.log(detail);
    },
    showPopup() {
        this.setData({ show: true });
    },
    // 确认选择地址
    onConfirm(event) {
        var text = event.detail.values; // 获取选中的地址信息
        console.log(text);
        var addres = this.data.myAddress
        addres.provinceCode = text[0].code
        addres.province = text[0].name
        addres.cityCode = text[1].code
        addres.city = text[1].name
        addres.district = text[2].name
        addres.districtCode = text[2].code
        this.setData({
            myAddress: addres
        })
        var address = ""
        address = addres.province + "," + addres.city + "," + addres.district
        this.setData({
            address: address
        })
        console.log(this.data.myAddress);
        this.setData({ show: false }); // 关闭弹窗
    },

    onClose() {
        this.setData({ show: false });
    },
    yes() {
        var name = this.data.name
        var phone = this.data.phone
        var details = this.data.details

        if (name == '' || phone == '' || details == '' || this.data.myAddress.cityCode == '') {
            wx.showToast({
                title: '信息不完整',
                icon: 'error',
                duration: 1000
            })
        } else {
            var my = this.data.myAddress
            my.receiverName = name
            my.receiverPhone = phone
            my.details = details
            this.setData({
                myAddress: my
            })
            http.POST('/user/address', my, function (res) {
                if (res.code === 200) {
                    wx.showToast({
                        title: '添加成功',
                        duration: 1000
                    })

                    setTimeout(() => {
                        wx.reLaunch({
                            url: '/my/pages/mzhAddress/mzhAddress'
                        })
                    }, 1500)
                }
                console.log('成功:', res);
            }, function (err) {
                console.error('失败:', err);
            });


        }

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options.userId);
        this.setData({
            "myAddress.userId": options.userId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})